package com.yiren.dbaa.module.practice_set.map.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.map.contract.BindLineContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;

import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/12/26
 */
public class BindLineModel implements BindLineContract.Model {

    private Activity activity;
    private UserParamConfigBean userParamConfig;
    private Gson gs;

    public BindLineModel(Activity activity) {
        this.activity = activity;
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
        gs = new Gson();
    }

    @Override
    public void getUserAllLine(CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadUserAllLine("?userId=" + userParamConfig.getUserInfo().getId()), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<LineInfoEntity>> testBaseBean = gs.fromJson(result, new TypeToken<TestBaseBean<List<LineInfoEntity>>>() {
                }.getType());
                if (testBaseBean.isSuccess()) {
                    List<LineInfoEntity> lineInfos = testBaseBean.getData();
                    if (null == lineInfos || lineInfos.size() == 0) {
                        callBack.fail("线路列表为空");
                    } else {
                        callBack.success(testBaseBean.getData());
                    }
                } else {
                    callBack.fail("线路列表加载失败！");
                }
            }
        });
    }

}